//
//  BrandList.swift
//  shop
//
//  Created by chen on 2025/5/18.
//

import SwiftUI
import Kingfisher

struct BrandList: View {
    
    var brand: BrandItem
    
    @ObservedObject var router: Router
    
    var body: some View {
        VStack(spacing: 0) {
            HStack(alignment: .center, spacing: 10){
                HStack(alignment: .top, spacing: 0){
                    KFImage.url(URL(string: brand.brandLogo))
                           .loadDiskFileSynchronously()
                           .cacheMemoryOnly()
                           .fade(duration: 0.35)
                           .resizable()
                           .aspectRatio(contentMode: .fill)
                }
                .padding(8)
                .frame(width: 80)
                
                VStack(alignment: .leading, spacing: 5) {
                    HStack{
                        Text("\(brand.brandName)")
                            .font(.system(size: 17))
                            .bold()
                        
                        Spacer()
                        
                        Text("\(brand.brandFeatures)")
                            .font(.system(size: 10))
                            .foregroundStyle(.black.opacity(0.5))
                            .lineLimit(1)
                            .minimumScaleFactor(0.5)
                    }
                    
                    HStack{
                        Text("领券最高立减\(brand.maxDiscountAmount.formatMoney())")
                        Text("|")
                        Text("单品低至\(brand.maxDiscount.formatMoney())折")
                    }
                    .font(.system(size: 14))
                    .foregroundStyle(Color.brandBg)
                }
            }
            
            HStack(spacing: 0) {
                Text("热销商品")
                    .font(.system(size: 14))
                    .padding(.vertical, 10)
                
                Spacer()
                
                Text("已售\(brand.sales)件")
                    .font(.system(size: 13))
                    .foregroundStyle(Color.black.opacity(0.8))
                    .lineLimit(1)
                    .minimumScaleFactor(0.5)
            }
            .padding(.leading, 10)
            
            ScrollView(.horizontal, showsIndicators: false) {
                LazyHStack{
                    ForEach(brand.goodsList, id: \.id) { item in
                        Button {
                            router.to(.detailView(id: item.id))
                        } label: {
                            BrandCard(item: item)
                        }
                        .buttonStyle(PlainButtonStyle())
                    }
                }
            }
            .padding(.vertical, 10)
            .padding(.horizontal, 5)
        }
        .padding(.horizontal, 10)
        .padding(.vertical, 10)
        .background(.white)
        .cornerRadius(10)
        .padding(.horizontal, 10)
    }
}
